package com.sinsoft.monitorDemo.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import redis.clients.jedis.Jedis;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.sinsoft.monitorDemo.entity.NetInfo;
import com.sinsoft.monitorDemo.entity.NetIo;
import com.sinsoft.monitorDemo.util.JsonUtil;
import com.sinsoft.monitorDemo.util.RedisUtil;

public class NetInfoDao {
	
	/**
	 * 通过IP的到网络状态信息
	 * @param ip
	 * @param index
	 * @return
	 */
	public List<NetInfo> getNetInfoList(String ip){
		Jedis jedis = RedisUtil.getJedis();
		List<String> jsonList = jedis.lrange("StatusData_" + ip + "_NET_latest", 0, -1);
		JsonArray jsonArray = null;
		JsonObject jsonObject = null;
		NetInfo netInfo = null;
		NetIo netIo =null;
		List<NetInfo> netInfoList = new ArrayList<NetInfo>();
		if (jsonList.size()>10) {
			for (int i = jsonList.size()-10; i < jsonList.size(); i++) {
				Map<String, NetIo> netIomap = new HashMap<String, NetIo>();
				jsonArray = JsonUtil.JsonParse(jsonList.get(i));
				jsonObject = jsonArray.get(0).getAsJsonObject();
				JsonObject netIoJson = (JsonObject)jsonObject.get("net_io");
				Set<Entry<String, JsonElement>> entrySet = netIoJson.entrySet();
				for (Entry<String, JsonElement> entry : entrySet) {
					JsonArray value = (JsonArray)entry.getValue();
					netIo = new NetIo(value.get(0).getAsDouble(), value.get(1).getAsDouble());
					netIomap.put(entry.getKey(), netIo);
				}
				JsonObject tcpStatus = (JsonObject)jsonObject.get("tcp_status"); 
				netInfo = new NetInfo(netIomap, tcpStatus.get("TIME_WAIT").getAsInt(), 
						tcpStatus.get("CLOSE_WAIT").getAsInt(), tcpStatus.get("ESTABLISHED").getAsInt(), 
						jsonArray.get(1).getAsLong(),ip);
				netInfoList.add(netInfo);
			}		
		} else {
			for (int i = 1; i < jsonList.size(); i++) {
				Map<String, NetIo> netIomap = new HashMap<String, NetIo>();
				jsonArray = JsonUtil.JsonParse(jsonList.get(i));
				jsonObject = jsonArray.get(0).getAsJsonObject();
				JsonObject netIoJson = (JsonObject)jsonObject.get("net_io");
				Set<Entry<String, JsonElement>> entrySet = netIoJson.entrySet();
				for (Entry<String, JsonElement> entry : entrySet) {
					JsonArray value = (JsonArray)entry.getValue();
					netIo = new NetIo(value.get(0).getAsDouble(), value.get(1).getAsDouble());
					netIomap.put(entry.getKey(), netIo);
				}
				JsonObject tcpStatus = (JsonObject)jsonObject.get("tcp_status"); 
				netInfo = new NetInfo(netIomap, 
						tcpStatus.get("TIME_WAIT").getAsInt(), tcpStatus.get("CLOSE_WAIT").getAsInt(), 
						tcpStatus.get("ESTABLISHED").getAsInt(), jsonArray.get(1).getAsLong(),ip);
				netInfoList.add(netInfo);
			}	
		}
		jedis.close();
		return netInfoList;		
	}
	
	/**
	 * 得到最新的网络状态信息
	 * @param ip
	 * @return
	 */
	public NetInfo getNewNetInfo(String ip){
		Jedis jedis = RedisUtil.getJedis();
		NetInfo netInfo;
		NetIo netIo;
		try {
			List<String> jsonList = jedis.lrange("StatusData_" + ip + "_NET_latest", -1, -1);
				Map<String, NetIo> netIomap = new HashMap<String, NetIo>();
				JsonArray jsonArray = JsonUtil.JsonParse(jsonList.get(0));
				JsonObject jsonObject = jsonArray.get(0).getAsJsonObject();
				JsonObject netIoJson = (JsonObject)jsonObject.get("net_io");
				Set<Entry<String, JsonElement>> entrySet = netIoJson.entrySet();
				for (Entry<String, JsonElement> entry : entrySet) {
					JsonArray value = (JsonArray)entry.getValue();
					netIo = new NetIo(value.get(0).getAsDouble(), value.get(1).getAsDouble());
					netIomap.put(entry.getKey(), netIo);
				}
				JsonObject tcpStatus = (JsonObject)jsonObject.get("tcp_status"); 
				netInfo = new NetInfo(netIomap, 
						tcpStatus.get("TIME_WAIT").getAsInt(), tcpStatus.get("CLOSE_WAIT").getAsInt(), 
						tcpStatus.get("ESTABLISHED").getAsInt(), jsonArray.get(1).getAsLong(),ip);
		} catch (Exception e) {
			return new NetInfo(null, null, null, null, null,null);
		}finally{
			jedis.close();	
		}
		return netInfo;	
	}

}
